package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.*;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface EquipmentMapper {
    @Insert("INSERT INTO `equipment`" +
            " (`tenant_id`,`name`,`version`,`vendor`,`manufacturing_coding`,`manufacturing_time`,`enable_time`,`maintain_group`,`temporary_coding`,`current_coding`,`remark`,`ok`,`is_exist`,`is_automation`,`is_pivotal`) VALUES" +
            " (#{tenantId},#{name},#{version},#{vendor},#{manufacturingCoding},#{manufacturingTime},#{enableTime},#{maintainGroup},#{temporaryCoding},#{currentCoding},#{remark},#{ok},#{isExist},#{isAutomation},#{isPivotal})")
    void create(Equipment equipment);

    @Select("<script>" +
            "SELECT * FROM `equipment` " +
            "<where>" +
            " `tenant_id`=#{equipment.tenantId} " +
            "<if test='equipment.id!=null'> AND `id`=#{equipment.id}</if>" +
            "<if test='equipment.name!=null'> AND `name` LIKE CONCAT('%',#{equipment.name},'%') </if>" +
            "<if test='equipment.maintainGroup!=null'> AND `maintain_group`!=#{equipment.maintainGroup}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " AND `tenant_id`=#{equipment.tenantId} " +
            " ORDER BY `id` LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Equipment> findAll(@Param("equipment") Equipment equipment, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT count(*) FROM `equipment` " +
            "<where>" +
            " `tenant_id`=#{equipment.tenantId} " +
            "<if test='equipment.id!=null'> AND `id`=#{equipment.id}</if>" +
            "<if test='equipment.name!=null'> AND `name` LIKE CONCAT('%',#{equipment.name},'%') </if>" +
            "<if test='equipment.maintainGroup!=null'> AND `maintain_group`!=#{equipment.maintainGroup}</if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " AND `tenant_id`=#{equipment.tenantId} " +
            "</script>")
    long count(@Param("equipment") Equipment equipment);

    @Update("UPDATE equipment SET " +
            "`name` = #{equipment.name}" +
            ",`version` = #{equipment.version}" +
            ",`vendor` = #{equipment.vendor}" +
            ",`manufacturing_coding` = #{equipment.manufacturingCoding}" +
            ",`manufacturing_time` = #{equipment.manufacturingTime}" +
            ",`enable_time` = #{equipment.enableTime}" +
            ",`maintain_group` = #{equipment.maintainGroup}" +
            ",`temporary_coding` = #{equipment.temporaryCoding}" +
            ",`current_coding` = #{equipment.currentCoding}" +
            ",`remark` = #{equipment.remark}" +
            ",`ok` = #{equipment.ok}" +
            ",`is_exist` = #{equipment.isExist}" +
            ",`is_automation` = #{equipment.isAutomation}" +
            ",`is_pivotal` = #{equipment.isPivotal}" +
            " WHERE `id` = #{equipment.id} AND `tenant_id` = #{equipment.tenantId}")
    int update(@Param("equipment") Equipment equipment);

    @Update("UPDATE `equipment` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int delete(@Param("id") long id,@Param("tenantId") long tenantId);


    @Select("SELECT * FROM equipment " +
            "WHERE `id` = #{id} AND `status`!=" + User.STATUS_DELETE)
    Equipment findById(@Param("id") long id);
}
